#!/bin/bash
export HOMEDIR=$(dirname $(dirname $(readlink -f "${BASH_SOURCE[0]}")))
source $HOMEDIR/scripts/init-env.sh

if [ -f "$ptk_global_base_dir/app/lib/postgresql/oracle_fdw.so" ]; then
    echo "文件 oracle_fdw.so 已存在。无需安装"
else 
unzip -o -j $HOMEDIR/$pkg_oracle_client -d $HOMEDIR/workspace/oracle-client
cp -r -n $HOMEDIR/workspace/oracle-client/libclntsh.so.19.1 $ptk_global_base_dir/app/lib/
cp -r -n $HOMEDIR/workspace/oracle-client/libnnz19.so $ptk_global_base_dir/app/lib/
cp -r -n $HOMEDIR/workspace/oracle-client/libclntshcore.so.19.1 $ptk_global_base_dir/app/lib/
cp -r -n $HOMEDIR/workspace/oracle-client/libociei.so $ptk_global_base_dir/app/lib/

chown $ptk_global_user: $ptk_global_base_dir/app/lib/libclntsh.so.19.1
chown $ptk_global_user: $ptk_global_base_dir/app/lib/libnnz19.so
chown $ptk_global_user: $ptk_global_base_dir/app/lib/libclntshcore.so.19.1
chown $ptk_global_user: $ptk_global_base_dir/app/lib/libociei.so

$OSEXEC gs_guc generate -D $ptk_global_base_dir/app/bin -o usermapping -S default
ptk cluster install-plugin -n $ptk_global_cluster_name -p $HOMEDIR/$pkg_oracle_fdw
fi

exists_oracle_fdw=`$OSEXEC $GSQLEXEC -t -A -c "select count(1) from pg_extension where extname='oracle_fdw';"`

if [ $exists_oracle_fdw == 0 ]; then
$OSEXEC $GSQLEXEC -c "set behavior_compat_options='';create extension oracle_fdw;"
fi